package com.labichaoka.chaoka.ui.baseinfo.real;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseActivity;
import com.labichaoka.chaoka.base.GlobalParams;
import com.labichaoka.chaoka.entity.BaseResponse;
import com.labichaoka.chaoka.entity.RealInfoRequest;
import com.labichaoka.chaoka.ui.bank.bind.BankBindActivity;
import com.labichaoka.chaoka.utils.DetectionUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class RealInfoActivity extends BaseActivity implements RealInfoView {

    @BindView(R.id.icon3)
    TextView icon3;
    @BindView(R.id.icon1)
    TextView icon1;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.icon2)
    TextView icon2;
    @BindView(R.id.card)
    EditText card;
    @BindView(R.id.commit)
    Button commit;

    private String nameStr;
    private String cardStr;

    private RealInfoPresenter presenter;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_real_info;
    }

    @Override
    protected void initView() {
        icon1.setTypeface(iconfont);
        icon2.setTypeface(iconfont);
        icon3.setTypeface(iconfont);
    }

    @Override
    protected void initData() {
        presenter = new RealInfoPresenterImpl(this, new RealInfoInteractorImpl());
    }

    @OnClick(R.id.commit)
    public void click(View v) {
        RealInfoRequest realInfoRequest = new RealInfoRequest();
        realInfoRequest.setRealName(nameStr);
        if (cardStr.contains("x")){
            cardStr=cardStr.replaceAll("x","X");
        }
        LogUtils.d(cardStr);
        realInfoRequest.setIdCardNo(cardStr);
        presenter.submitRealNameAuth(realInfoRequest);
        presenter.behavior(GlobalParams.CLICK_REALNAME_AUTH);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                nameStr = editable.toString();
                checkBtnStatus();
            }
        });
        card.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                cardStr = editable.toString();
                checkBtnStatus();
            }
        });

    }

    private void checkBtnStatus() {
        if (TextUtils.isEmpty(nameStr)||TextUtils.isEmpty(cardStr)){
            commit.setEnabled(false);
            return;
        }
        if (!DetectionUtil.isNameChina(nameStr) || !DetectionUtil.isIdCard(cardStr)) {
            commit.setEnabled(false);
        }else{
            commit.setEnabled(true);
        }
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingProgress();
    }

    @Override
    public void submitRealNameAuthCallback(BaseResponse response) {
        Bundle bundle=new Bundle();
        bundle.putInt("type",GlobalParams.BIND_BANK_CARD);
        gotoActivity(mContext, BankBindActivity.class, bundle);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}
